package lc.q51_100;


public class Q58 {

    public int lengthOfLastWord(String s) {
        int lastUnSpace = -1;
        for (int i = s.length() - 1; i >=0; i--) {
            if (s.charAt(i) != ' ') {
                lastUnSpace = i;
                break;
            }
        }
        if (lastUnSpace == -1) {
            return 0;
        }
        for (int unSpace = lastUnSpace; unSpace >= 0; unSpace--) {
            if (s.charAt(unSpace) == ' ') {
                return lastUnSpace - unSpace;
            }
        }
        return lastUnSpace + 1;
    }

    public static void main(String[] args) {
        System.out.println(new Q58().lengthOfLastWord(" Hello"));
    }

}
